package com.bh.base.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.base.domain.BhTimeLiness;
import com.bh.base.mapper.BhTimeLinessMapper;
import com.bh.base.service.IBhTimeLinessService;
import com.bh.common.annotation.DataDictClass;
import com.bh.common.exception.base.BaseException;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 距离时效Service业务层处理
 *
 * @author slx
 * @date 2023-07-15
 */
@Service
public class BhTimeLinessServiceImpl extends ServiceImpl<BhTimeLinessMapper, BhTimeLiness> implements IBhTimeLinessService {
    @Autowired
    private BhTimeLinessMapper bhTimeLinessMapper;

    /**
     * 查询距离时效
     *
     * @param id 距离时效主键
     * @return 距离时效
     */
    @DataDictClass
    @Override
    public BhTimeLiness selectJcTimeLinessById(Long id) {
        return bhTimeLinessMapper.selectJcTimeLinessById(id);
    }

    @Override
    public BhTimeLiness selectJcTimeLinessByOrgs(Long startOrgId, Long destineOrgId) {
        return bhTimeLinessMapper.selectJcTimeLinessByOrgs(startOrgId, destineOrgId);
    }

    /**
     * 查询距离时效列表
     *
     * @param bhTimeLiness 距离时效
     * @return 距离时效
     */
    @DataDictClass
    @Override
    public List<BhTimeLiness> selectJcTimeLinessList(BhTimeLiness bhTimeLiness) {
        return bhTimeLinessMapper.selectJcTimeLinessList(bhTimeLiness);
    }

    /**
     * 新增距离时效
     *
     * @param bhTimeLiness 距离时效
     * @return 结果
     */
    @Override
    public int insertJcTimeLiness(BhTimeLiness bhTimeLiness) {
        // 不允许重复
        BhTimeLiness old = bhTimeLinessMapper.selectJcTimeLinessByOrgs(bhTimeLiness.getStartOrgId(), bhTimeLiness.getDestineOrgId());
        if (old != null) {
            throw new BaseException("该机构已存在该距离时效");
        }
        if (Objects.equals(bhTimeLiness.getStartOrgId(), bhTimeLiness.getDestineOrgId())) {
            throw new BaseException("起始机构与目的机构不能相同");
        }
        bhTimeLiness.setCreateBy(SecurityUtils.getUsername());
        bhTimeLiness.setCreateTime(DateUtils.getNowDate());
        return bhTimeLinessMapper.insert(bhTimeLiness);
    }

    /**
     * 修改距离时效
     *
     * @param bhTimeLiness 距离时效
     * @return 结果
     */
    @Override
    public int updateJcTimeLiness(BhTimeLiness bhTimeLiness) {
        // 不允许重复
        BhTimeLiness old = bhTimeLinessMapper.selectJcTimeLinessByOrgs(bhTimeLiness.getStartOrgId(), bhTimeLiness.getDestineOrgId());
        if (old != null && !old.getId().equals(bhTimeLiness.getId())) {
            throw new BaseException("该机构已存在该距离时效");
        }
        return bhTimeLinessMapper.updateById(bhTimeLiness);
    }

    /**
     * 批量删除距离时效
     *
     * @param ids 需要删除的距离时效主键
     * @return 结果
     */
    @Override
    public int deleteJcTimeLinessByIds(Long[] ids) {
        return bhTimeLinessMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除距离时效信息
     *
     * @param id 距离时效主键
     * @return 结果
     */
    @Override
    public int deleteJcTimeLinessById(Long id) {
        return bhTimeLinessMapper.deleteById(id);
    }
}
